package com.bjtu.ptsite.tracker.service;

import com.bjtu.ptsite.utils.RedisUtil;
import com.turn.ttorrent.client.SharedTorrent;
import com.turn.ttorrent.common.protocol.TrackerMessage;
import com.turn.ttorrent.tracker.TrackedPeer;
import com.turn.ttorrent.tracker.TrackedTorrent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;


/**
 *
 * @author Zehao Wang, Junjie Zhang, Yutao Wei
 *
 */
public class LoggingTrackedTorrentService extends TrackedTorrent {


    public LoggingTrackedTorrentService(SharedTorrent torrent) throws IOException, NoSuchAlgorithmException {
        super(torrent);
    }


    @Override
    public TrackedPeer update(TrackerMessage.AnnounceRequestMessage.RequestEvent event, ByteBuffer peerId,
                              String hexPeerId, String ip, int port,
                              long uploaded, long downloaded, long left)
            throws UnsupportedEncodingException {

        TrackedPeer peer = super.update(event, peerId, hexPeerId, ip, port, uploaded, downloaded, left);
        // 假设你能从当前对象访问到这个torrent，或者自己保存
        byte[] infoHash = this.getInfoHash();

        // 连接数（Peer数量）用Redis Set存储所有peerId，方便统计

        return peer;
    }
}
